<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

/**
 * 课程管理区别于course
 * @author LIYANG
 *
 */
class manage extends admin{
	private $db;
	private $agency_db;
	public $siteid;
	public $admin_userid;
	public $type;
	
	public function __construct() {
		parent::__construct();
		pc_base::load_app_func('global');
		$this->db = pc_base::load_model('course_nsr_model');//加载课程model
		$this->agency_db = pc_base::load_model('agency_nsr_model');//加载机构model
		$this->course_source_db = pc_base::load_model('agency_course_source_model');//加载课程关联资源模型
		$this->video_db = pc_base::load_model('videos_model');//加载视频集模型
		$this->course_sign_db = pc_base::load_model('course_sign_model');//加载课程报名信息模型
		$this->course_talent_db = pc_base::load_model('course_talent_model');//加载课程关联人才模型
		$this->siteid = get_siteid();
		$this->admin_userid = param::get_cookie('userid');//获取当前登录的管理员ID
		$model_setting=pc_base::load_model('site_setting_model');
		$this->type= $model_setting->getValue("course_nsr_class",$this->siteid);//获取课程班制
	}
	
	public function init() {
		$condition = "";
		$condition.= "siteid = ".$this->siteid." and status = 1";
		$agency = $this->agency_db->select($condition,'id,full_name');//获取机构
		$agency_new= array();
		foreach ($agency as $key=>$value){
			$agency_new[$value['id']] = $value['full_name'];
		}
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$where = "";
		$where .= "siteid = ".$this->siteid." and status = 1";
		$search_info = $_GET['course'];
		if ($search_info) {
			if (!empty($search_info['agency'])) {
				$where .= " and agency_id = ".$search_info['agency'];
			}
			if(!empty($search_info['name'])){
				$keyword = $search_info['name'];
				$where .= " and name like '%$keyword%'";
			}
		}
		$course_info = $this->db->listinfo($where,'id DESC',$page,10);
		$admin_model = pc_base::load_model('admin_model');//加载管理员model
		$adminInfo = $admin_model->getRealnameInfo();
		foreach ($course_info as $k=>$v){
			//获取用户管理员真实名称数据
			$course_info[$k]['username'] = $adminInfo[$v['updated_by']];
		}
		$pages = $this->db->pages;
		pc_base::load_sys_class('form','',0);
		pc_base::load_sys_class('format','',0);
		include $this->admin_tpl('course_nsr_list');
	}
	
	public function add() {
		if (isset($_POST['dosubmit'])) {
			$course = isset($_POST['course']) ? $_POST['course'] : showmessage(L('error_return'),HTTP_REFERER);
			$data['agency_id'] = isset($course['agency']) ? $course['agency'] : showmessage(L('please_select_course_agency'),HTTP_REFERER);
			$data['name'] = trim($course['name']);
			$data['object'] = $course['object'];
			$data['target'] = $course['target'];
			$data['content'] = $course['content'];
			$data['image_horizontal'] = $course['image_horizontal'];
			$data['image_vertical'] = $course['image_vertical'];
			$data['image_square'] = $course['image_square'];
			$data['course_day'] = isset($course['day']) ? implode(',',$course['day']) : showmessage(L('please_select_day'),HTTP_REFERER);
			$data['course_time'] = $course['time'];
			$data['course_period'] = $course['period'];//course_period 为：课时/学时
			$data['period'] = $course['period_zhouqi'];//period 为：周期
			$data['price'] = trim($course['price']);
			$data['special'] = $course['special'];
			$data['create_time'] = $data['update_time'] = date('Y-m-d H:i:s',time());
			$data['created_by'] = $data['updated_by'] = $this->admin_userid;
			$data['siteid'] = $this->siteid;
			$data['status'] = 1;
			$result = $this->db->insert($data,true);
			if ($result) {
				//记录行为日志
				$log_content = '<span style="color:green;">添加课程</span>[课程id：'.$result.'，课程名称：'.$course['name'].']';
				$this->manage_admin_log($log_content);
				showmessage(L('course_add_success'),'?m=course_nsr&c=manage&a=init');
			} else {
				showmessage(L('course_add_error'),HTTP_REFERER);
			}
		} else {
			$where = "";
			$where .= "siteid = ".$this->siteid." and status = 1";
			$agency = $this->agency_db->select($where,'id,full_name');//获取机构
			$agency_new= array();
			foreach ($agency as $key=>$value){
				$agency_new[$value['id']] = $value['full_name'];
			}
			pc_base::load_sys_class('form','',0);
			pc_base::load_sys_class('format');
			include $this->admin_tpl('course_nsr_add');
		}
	}
	
	public function edit() {
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message_not_exits'),HTTP_REFERER);
		if (isset($_POST['dosubmit'])) {
			$course = isset($_POST['course']) ? $_POST['course'] : showmessage(L('error_return'),HTTP_REFERER);
			$data['agency_id'] = isset($course['agency']) ? $course['agency'] : showmessage(L('please_select_course_agency'),HTTP_REFERER);
			$data['name'] = trim($course['name']);
			$data['object'] = $course['object'];
			$data['target'] = $course['target'];
			$data['type'] = $course['type'];
			$data['content'] = $course['content'];
			$data['image_horizontal'] = $course['image_horizontal'];
			$data['image_vertical'] = $course['image_vertical'];
			$data['image_square'] = $course['image_square'];
			$data['course_day'] = isset($course['day']) ? implode(',',$course['day']) : showmessage(L('please_select_day'),HTTP_REFERER);
			$data['course_time'] = $course['time'];
			$data['course_period'] = $course['period'];//course_period 为：课时/学时
			$data['period'] = $course['period_zhouqi'];//period 为：周期
			$data['price'] = trim($course['price']);
			$data['special'] = $course['special'];
			$data['update_time'] = date('Y-m-d H:i:s',time());
			$data['updated_by'] = $this->admin_userid;
			$data['siteid'] = $this->siteid;
			$result = $this->db->update($data,array('id'=>$id));
			if ($result) {
				//记录行为日志
				$log_content = '<span style="color:blue;">修改课程</span>[课程id：'.$id.'，课程名称：'.$course['name'].']';
				$this->manage_admin_log($log_content);
				showmessage(L('course_edit_success'),'?m=course_nsr&c=manage&a=init');
			} else {
				showmessage(L('course_edit_error'),HTTP_REFERER);
			}
		} else {
			$where = "";
			$where .= "siteid = ".$this->siteid." and status = 1";
			$agency = $this->agency_db->select($where,'id,full_name');//获取机构
			$agency_new= array();
			foreach ($agency as $key=>$value){
				$agency_new[$value['id']] = $value['full_name'];
			}
			$course_info = $this->db->get_one(array('id'=>$id));
			pc_base::load_sys_class('form','',0);
			$show_header = false;
			include $this->admin_tpl('course_nsr_edit');
		}
	}
	
	public function del() {
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
			showmessage(L('message_not_exits'), HTTP_REFERER);
		}
		$id = $_REQUEST['id'];
		if (is_array($id)) {
			foreach ($id as $k => $v) {
				if (intval($v)) {
					$id[$k] = intval($v);
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'', $id);
		}
		$result = $this->db->update(array(
				'status' => '2'
		), "id in ('$id')");//删除课程
		$result_source = $this->course_source_db->deleteCoursesource($id);//删除课程关联的资讯、视频、图片集
		$result_video = $this->video_db->deleteCoursevideo($id);//删除课程关联的视频集
		$result_sign = $this->course_sign_db->deleteCoursesign($id);//删除课程下的报名信息
		$result_talent = $this->course_talent_db->deleteCoursetalent($id);//删除课程关联的人才
		if ($result && $result_source && $result_video && $result_sign && $result_talent) {
			$del_id = explode('\',\'', $id);
			$new_id = implode('，', $del_id);
			//记录行为日志
			$log_content = '<span style="color:red;">删除课程</span>[课程id：'.$new_id.']';
			$this->manage_admin_log($log_content);
			showmessage(L('course_del_success'), '?m=course_nsr&c=manage&a=init');
		} else {
			showmessage(L('course_del_error'), HTTP_REFERER);
		}
	}
	
	public function detail() {
		$condition = "";
		$condition.= "siteid = ".$this->siteid." and status = 1";
		$agency = $this->agency_db->select($condition,'id,full_name');//获取机构
		$agency_new= array();
		foreach ($agency as $key=>$value){
			$agency_new[$value['id']] = $value['full_name'];
		}
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message_not_exits'),HTTP_REFERER);
		$course_info = $this->db->get_one(array('id'=>$id));
		pc_base::load_sys_class('form', '', 0);
		$show_header = false;
		include $this->admin_tpl('course_nsr_detail');
	}
}